package org.sang.service;

import com.mysql.jdbc.StringUtils;
import org.sang.bean.Employee;
import org.sang.bean.User;
import org.sang.mapper.RoleMapper;
import org.sang.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by daigb on 2018/02/01.
 */
@Service
@Transactional
public class UserService implements UserDetailsService {

    @Autowired
    UserMapper userMapper;
    SimpleDateFormat birthdayFormat = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userMapper.loadUserByUsername(s);
        if (user == null) {
            throw new UsernameNotFoundException("用户名不对");
        }
        return user;
    }

    public int userReg(User user) {
        //如果用户名存在，返回错误
        if (userMapper.loadUserByUsername(user.getUsername()) != null) {
            return -1;
        }
        String password=user.getPassword();
        if(StringUtils.isNullOrEmpty(password)){
            password="123456";
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(password);
        user.setPassword(encode);
        Long ret= userMapper.userReg(user);
        //赋普通角色
        return updateUserRoles(user.getId(),new Long[]{1L});
    }

    public List<User> getUsersByKeywords(String keywords) {
        return userMapper.getUsersByKeywords(keywords);
    }

    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    public int updateUserRoles(Long userId, Long[] rids) {
        int i = userMapper.deleteRoleByUserId(userId);
        return userMapper.addRolesForUser(userId, rids);
    }

    public User getUserById(Long userId) {
        return userMapper.getUserById(userId);
    }

    public int deleteUser(Long userId) {
        return userMapper.deleteUser(userId);
    }

    public List<User> getUserByPage(Integer page, Integer size, String keywords,  String company,String beginDateScope) {
        int start = (page - 1) * size;
        Date startBeginDate = null;
        Date endBeginDate = null;
        if (beginDateScope != null && beginDateScope.contains(",")) {
            try {
                String[] split = beginDateScope.split(",");
                startBeginDate = birthdayFormat.parse(split[0]);
                endBeginDate = birthdayFormat.parse(split[1]);
            } catch (ParseException e) {
            }
        }
        return userMapper.getUserByPage(start, size, keywords, company, startBeginDate, endBeginDate);
    }

    public Long getCountByKeywords(String keywords, String company,String beginDateScope) {
        Date startBeginDate = null;
        Date endBeginDate = null;
        if (beginDateScope != null && beginDateScope.contains(",")) {
            try {
                String[] split = beginDateScope.split(",");
                startBeginDate = birthdayFormat.parse(split[0]);
                endBeginDate = birthdayFormat.parse(split[1]);
            } catch (ParseException e) {
            }
        }
        return userMapper.getCountByKeywords(keywords, company, startBeginDate, endBeginDate);
    }
}
